import store from '@/store'
import { jsonsToMultipleSheetXlsx } from '@/utils/lib/Export2Excel'
import type { SummaryModel } from '@/views/statistics/types'
interface StatisticsXlsxItem {
  name: string
  header: SummaryModel
  data: SummaryModel[]
}
export function useStatisticsXlsx(fileName: string) {
  // TODO 缺少院内人员和患者打卡率这种与时间无关的
  const { header: header1, data: data1, name: name1 } = getSummaryList()
  const { header: header2, data: data2, name: name2 } = getOverviewList()
  const { header: header3, data: data3, name: name3 } = getTreatMentLevel()
  const {
    header: header4,
    data: data4,
    name: name4,
  } = getDiagnosticStatistics()
  const { header: header5, data: data5, name: name5 } = getAgeDistribution()
  const { header: header6, data: data6, name: name6 } = getAsthmaInducement()
  jsonsToMultipleSheetXlsx({
    datas: [data1, data2, data3, data4, data5, data6],
    headers: [header1, header2, header3, header4, header5, header6],
    filenames: [name1, name2, name3, name4, name5, name6],
    xlsxName: fileName,
  })
}

function getSummaryList(): StatisticsXlsxItem {
  // const data = store.getters.summaryList
  function getData() {
    return store.getters.summaryList.concat([
      { name: '打卡数', value: store.getters.markedCard.marked + '' },
    ])
  }
  return {
    name: '概览',
    header: { name: '导出名称', value: '数量' },
    data: getData(),
  }
}

function getOverviewList(): StatisticsXlsxItem {
  // const data = store.getters.overviewList
  function getData() {
    const value = (store.getters.markedCard.visitRate * 100).toFixed(2) + '%'
    return store.getters.overviewList.concat([
      { name: '累计复诊率', value },
      { name: '累计确诊人数', value: store.getters.diagnosed + '' },
    ])
  }
  return {
    name: '数据总览',
    header: { name: '导出名称', value: '数量' },
    data: getData(),
  }
}

function getTreatMentLevel(): StatisticsXlsxItem {
  const { good, goodRate, part, partRate, bad, badRate } =
    store.getters.treatmentLevel

  return {
    name: '治疗级别',
    header: { name: '导出名称', value: '比例' },
    data: [
      { value: good, name: '控制良好率' },
      { value: part, name: '部分控制率' },
      { value: bad, name: '未控制率' },
    ],
  }
}

function getDiagnosticStatistics(): StatisticsXlsxItem {
  const {
    bronchialAsthma,
    bronchialAsthma2,
    recurrentWheezingInPreschool,
    recurrentWheezingInPreschool2,
    coughVariantAsthma,
    coughVariantAsthma2,
    nonAsthma,
    acuteAttackOfBronchialAsthma,
    acuteOnsetOfRecurrentWheezingInPreschool,
  } = store.getters.diagnosticStatistics
  return {
    name: '诊断结果',
    header: { name: '导出名称', value: '结果' },
    data: [
      { value: bronchialAsthma, name: '支气管哮喘', id: '支气管哮喘' },
      {
        value: recurrentWheezingInPreschool,
        name: '学龄前反复喘息',
        id: '学龄前反复喘息',
      },
      {
        value: acuteAttackOfBronchialAsthma,
        name: '支气管哮喘急性发作',
        id: '支气管哮喘急性发作',
      },
      { value: bronchialAsthma2, name: '支气管哮喘？', id: '支气管哮喘？' },
      {
        value: acuteOnsetOfRecurrentWheezingInPreschool,
        name: '学龄前反复喘息急性发作',
        id: '学龄前反复喘息急性发作',
      },
      {
        value: recurrentWheezingInPreschool2,
        name: '学龄前反复喘息？',
        id: '学龄前反复喘息？',
      },
      {
        value: coughVariantAsthma,
        name: '咳嗽变异性哮喘(CVA)',
        id: '咳嗽变异性哮喘(CVA)',
      },
      {
        value: coughVariantAsthma2,
        name: '咳嗽变异性哮喘(CVA)？',
        id: '咳嗽变异性哮喘(CVA)？',
      },
      { value: nonAsthma, name: '无哮喘', id: '无哮喘' },
    ],
  }
}

function getAgeDistribution(): StatisticsXlsxItem {
  const { lt6, ge12, ge6Lt12 } = store.getters.ageDistribution
  return {
    name: '年龄分布',
    header: { name: '导出名称', value: '年龄分布' },
    data: [
      { value: lt6, name: 'x<6岁', id: 'x<6岁' },
      { value: ge6Lt12, name: '6岁≤x<12岁', id: '6岁≤x<12岁' },
      { value: ge12, name: '12岁≤x', id: '12岁≤x' },
    ],
  }
}
function getAsthmaInducement(): StatisticsXlsxItem {
  const {
    cold,
    strenuousExercise,
    yellLaugh,
    cryScream,
    sweetRefrigeration,
    weather,
    animal,
    smell,
  } = store.getters.asthmaInducement

  return {
    name: '哮喘诱因',
    header: { name: '导出名称', value: '哮喘诱因详情' },
    data: [
      { value: cold, name: '感冒', id: '感冒' },
      { value: strenuousExercise, name: '剧烈运动', id: '剧烈运动' },
      { value: yellLaugh, name: '大叫大笑', id: '大叫大笑' },
      { value: cryScream, name: '哭闹', id: '哭闹' },
      {
        value: sweetRefrigeration,
        name: '进食甜食或冷冻食品饮料',
        id: '进食甜食或冷冻食品饮料',
      },
      { value: weather, name: '气候变化', id: '气候变化' },
      { value: animal, name: '接触动物', id: '接触动物' },
      { value: smell, name: '闻到浓烈的气味', id: '闻到浓烈的气味' },
    ],
  }
}
